from django.db import models
from django.contrib.auth import get_user_model

User = get_user_model()  # 获取用户模型

class Bot(models.Model):  # 智能体
    user = models.ForeignKey(User, on_delete=models.CASCADE)  # 用户标识
    space_id = models.CharField(max_length=255, verbose_name="空间ID")
    name = models.CharField(max_length=255, verbose_name="智能体名称")
    description = models.TextField(verbose_name="描述")
    # icon_file_id = models.CharField(max_length=255, verbose_name="头像文件ID")
    prompt_info = models.TextField(verbose_name="提示信息")  # 提示信息字段
    onboarding_info = models.JSONField(default=dict, verbose_name="引导信息")  # 新增字段
    # suggested_questions = models.JSONField(verbose_name="建议的问题")
    bot_id = models.CharField(max_length=255, null=True, blank=True, verbose_name="智能体ID")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = "智能体"
        verbose_name_plural = "智能体"

class BotPublishHistory(models.Model):
    bot = models.ForeignKey(Bot, on_delete=models.CASCADE, verbose_name="智能体")  # 使用外键关联到 Bot 模型
    connector_ids = models.JSONField(verbose_name="发布渠道ID列表")
    publish_time = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")

    def __str__(self):
        return f"Publish history for bot {self.bot.name}"

    class Meta:
        verbose_name = "智能体发布历史"
        verbose_name_plural = "智能体发布历史"

class Conversation(models.Model):
    bot = models.ForeignKey(Bot, on_delete=models.CASCADE, verbose_name="智能体")  # 使用外键关联到 Bot 模型
    user_id = models.CharField(max_length=255, verbose_name="用户ID")
    user_message = models.TextField(verbose_name="用户消息")
    bot_response = models.TextField(verbose_name="智能体回复")
    stream_data = models.JSONField(verbose_name="流式数据", blank=True, null=True)  # 用于存储流式响应的数据
    stream_completed = models.BooleanField(default=False, verbose_name="流式完成状态")
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    create_note = models.BooleanField(default=False, verbose_name="是否生成笔记")
    session_id = models.CharField(max_length=255, verbose_name="会话ID", blank=True, null=True)  # 用于关联多轮对话

    def __str__(self):
        return f"Conversation with bot {self.bot_id}"

    class Meta:
        verbose_name = "对话"
        verbose_name_plural = "对话"